﻿using System.Collections;
using System;
using LitJson;
using System.IO;
using ProtoBuf;
using System.Text;

public static class LShare
{
    public static readonly Random random = new Random();

 
    public static T DeSerialize<T>(string content)
    {
       // using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(content))
        using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(content)))
        {
            T t = Serializer.Deserialize<T>(ms);
            return t;
        }
    }


    public static string Serialize<T>(T t)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.Serialize<T>(ms, t);
            return Convert.ToBase64String(ms.ToArray());

           // return Encoding.Default.GetString(ms.ToArray());
        }
    }

    public static CharDirect RandomDirect()
    {
        int randDirect = random.Next(0, 8);
        CharDirect direct = (CharDirect)randDirect;
        return direct;
    }
    //例如20,100,百分之20
    public static bool RandomRate(int start,int max)
    {
        if (start == max)
            return true;

        int reusult = random.Next(0, max);
        if (reusult < start)
            return true;
        else
            return false;
    }

    public static bool RandomSuccess(int total)
    {
        int reusult = random.Next(0, total);
        int middle = total / 2;
        if (reusult == middle)
            return true;
        else
            return false;
    }

    public static float real_cd(string skill,JsonData attPoint)
    {
        float staticCD = (float)(int)LStatic.cover_skill[skill]["cd"];//skill表里的CD值

        int point = (int)attPoint["cd"];//点数

        float value = Convert.ToSingle(LStatic.cover_attPoint["cd"]["value"].ToString());//每点增益

        float result = staticCD - point * value- (int)LStatic.cover_skill[skill]["liangongshi"]* Constant.CD_PER_DEL;
       
        if (result < 0)
            result = 0;
        return result;
    }


    public static Vector2i String2Vector2i(string str)
    {
        string[] arr = str.Split(',');
        return new Vector2i( Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]));
    }

    public static CharDirect CalDirectByMapPos(Vector2i mapPosStart, Vector2i mapPosEnd)
    {
        CharDirect direct;
        if ((int)mapPosStart.x == (int)mapPosEnd.x)
        {
            if (mapPosEnd.y > mapPosStart.y)
                direct = CharDirect.DIRECT_UP;
            else
                direct = CharDirect.DIRECT_DOWN;
        }
        else if ((int)mapPosStart.y == (int)mapPosEnd.y)
        {
            if (mapPosEnd.x > mapPosStart.x)
                direct = CharDirect.DIRECT_RIGHT;
            else
                direct = CharDirect.DIRECT_LEFT;
        }
        else if (mapPosStart.x < mapPosEnd.x && mapPosStart.y < mapPosEnd.y)
            direct = CharDirect.DIRECT_RIGHT_UP;
        else if (mapPosStart.x < mapPosEnd.x && mapPosStart.y > mapPosEnd.y)
            direct = CharDirect.DIRECT_RIGHT_DOWN;
        else if (mapPosStart.x > mapPosEnd.x && mapPosStart.y > mapPosEnd.y)
            direct = CharDirect.DIRECT_LEFT_DOWN;
        else
            direct = CharDirect.DIRECT_LEFT_UP;

        return direct;
    }
    public static void DirectToPos(CharDirect direct,out int posX,out int posY)
    {
        posX = 0;
        posY = 0;
        if (direct == CharDirect.DIRECT_UP)
            posY = 1;
        else if (direct == CharDirect.DIRECT_DOWN)
            posY = -1;
        else if (direct == CharDirect.DIRECT_RIGHT)
            posX = 1;
        else if (direct == CharDirect.DIRECT_LEFT)
            posX = -1;
        else if (direct == CharDirect.DIRECT_RIGHT_UP)
        {
            posX = 1;
            posY = 1;
        }
        else if (direct == CharDirect.DIRECT_RIGHT_DOWN)
        {
            posX = 1;
            posY = -1;
        }
        else if (direct == CharDirect.DIRECT_LEFT_DOWN)
        {
            posX = -1;
            posY = -1;
        }
        else
        {
            posX = -1;
            posY = 1;
        }
    }


    public static int JsonIndex(JsonData data,string find)
    {
        int index=0;
        foreach (JsonData jd in data)
        {
            if ((string)jd == find)
                return index;

            index++;
        }

        return -1;
    }
  

}